package com.atguigu.spzx.cart.controller;

import com.atguigu.spzx.cart.service.CartService;
import com.atguigu.spzx.model.entity.h5.CartInfo;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName: CartController
 * Package: com.atguigu.spzx.cart.controller
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/17 9:55
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/order/cart/auth")
public class CartController {
    @Resource
    private CartService cartService;
//    List<CartInfo> cartInfoList = cartFeignClient.getAllCkecked();

    /** /api/order/cart/auth/getAllCkecked
     * 根据token获取所有选中的购物车项
     * @return
     */
    @GetMapping("getAllCkecked")
    public List<CartInfo> getAllCkecked(){
        List<CartInfo> cartInfoList = cartService.getAllCkecked();
        return cartInfoList;
    }


    /**
     * /api/order/cart/auth/clearChecked
     * 清空选中的购物车项
     */
    @GetMapping("clearChecked")
    public void clearChecked(){
        cartService.clearChecked();
    }
    /**
     * 清空购物车
     * @return
     */
    @GetMapping("clearCart")
    public Result clearCart(){
        cartService.clearCart();
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 全选状态的修改
     * @param isChecked
     * @return
     */
    @GetMapping("/allCheckCart/{isChecked}")
    public Result allCheckCart(@PathVariable Integer isChecked) {
        cartService.allCheckCart(isChecked);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 跟新购物车项的选中状态
     *
     * @param skuId
     * @param isChecked
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId, @PathVariable Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 删除购物车项
     *
     * @param skuId
     * @return
     */
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId) {
        cartService.deleteCart(skuId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询购物车列表
     *
     * @return
     */
    @GetMapping("cartList")
    public Result cartList() {
        List<CartInfo> cartInfoList = cartService.cartList();
        return Result.build(cartInfoList, ResultCodeEnum.SUCCESS);
    }

    // http://localhost:8500/api/order/cart/auth/addToCart/1/1
    @GetMapping("addToCart/{skuId}/{count}")
    public Result addToCart(@PathVariable Long skuId, @PathVariable Integer count) {
        cartService.addToCart(skuId, count);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}
